/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solitairetest.logic;

import java.util.Vector;
import java.util.Random;

/**
 * @author Viktor
 */
public class Deck
{
    private Vector<Card> cards;

    public Deck()
    {
        cards = new Vector<Card>();
    }

    public void AddCard(Card c)
    {
        if (!cards.contains(c))
            cards.add(c);
    }

    public void FillPile()
    {
        this.FillPile(false);
    }

    public void FillPile(boolean facing)
    {
        for (int s = 0; s < 4; s++)
        {
            for (int r = 1; r < 14; r++)
            {
                try
                {
                    cards.add(new Card(Card.suits[s], r, facing));
                }
                catch (CardException e)
                {
                    System.out.println(e.getMessage());
                }
            }
        }
    }

    public void Shuffle()
    {
        Vector<Card> oldCards = cards;
        Vector<Card> newCards = new Vector<Card>();
        Random r = new Random(System.currentTimeMillis());

        while (oldCards.size() > 0)
        {
            newCards.add(oldCards.remove(r.nextInt(oldCards.size())));
        }

        cards = newCards;
    }

    public Card GetCard(int index)
    {
        return cards.elementAt(index);
    }

    public Deck GetCards(int count, int faceType)
    {
        Deck newDeck = new Deck();

        for (int i = 0; i < count; i++)
        {
            Card c = this.cards.get(i);

            if (faceType == 0)
                c.SetFacing(false);
            else if (faceType == 1)
                c.SetFacing(true);
            else if (faceType == 2)
            {
                if (i == count - 1)
                    c.SetFacing(true);
                else
                    c.SetFacing(false);
            }

            newDeck.AddCard(this.cards.get(i));
            this.cards.removeElementAt(i);
        }

        return newDeck;
    }

    public int Size()
    {
        return cards.size();
    }
}
